package com.yikaiye.android.yikaiye.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemMyCompanyBinding;
import com.yikaiye.android.yikaiye.model.CompanyRecordsModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.CompanyDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.UiUtils;

/**
 * 我的企业
 */
public class MyCompanyAdapter extends BaseQuickAdapter<CompanyRecordsModel.CompanyModel, DataBindingHolder<ItemMyCompanyBinding>> {

    private final Context context;

    public MyCompanyAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemMyCompanyBinding> itemChooseCompanyBindingDataBindingHolder, int i, @Nullable CompanyRecordsModel.CompanyModel companyModel) {
        ItemMyCompanyBinding binding = itemChooseCompanyBindingDataBindingHolder.getBinding();
        if (companyModel != null) {
            binding.tvCompanyName.setText(CommonUtil.checkText(companyModel.getEnterpriseName()));
            // 企业状态码（1：存续；2：注销；3：吊销；4：撤销，5：迁出，6：设立中，7：清算中，8：停业，9：其他）为空或者不在范围之内的都是其他
            binding.tvSetUpStatus.setText(UiUtils.getManagementStatus(companyModel.getNewStatusCode()));
            UiUtils.setManagementStatusColor(context, binding.tvSetUpStatus, companyModel.getNewStatusCode());
            // 服务状态：1.服务中 2.已结束
            if (companyModel.getServiceStatus() != null) {
                binding.tvAuthStatus.setVisibility(View.VISIBLE);
                binding.tvAuthStatus.setText(companyModel.getServiceStatus() == 1 ? context.getString(R.string.in_service) : context.getString(R.string.ended));
                binding.tvAuthStatus.setTextColor(ContextCompat.getColor(context, companyModel.getServiceStatus() == 1 ? R.color.color_2c7df1 : R.color.color_9b9b9b));
                binding.tvAuthStatus.setBackgroundResource(companyModel.getServiceStatus() == 1 ? R.drawable.shape_stroke_662c7df1_2dp : R.drawable.shape_stroke_669b9b9b_2dp);
            } else {
                binding.tvAuthStatus.setVisibility(View.GONE);
            }
            binding.llCode.setVisibility(View.GONE);
            binding.llContactInfo.setVisibility(View.GONE);
            binding.tvNoData.setVisibility(View.GONE);
            // 若企业没有信用代码，则不显示，信用代码超过18位也不展示
            if (!TextUtils.isEmpty(companyModel.getCreditNo()) && companyModel.getCreditNo().length() <= 18) {
                binding.llCode.setVisibility(View.VISIBLE);
                binding.tvCode.setText(companyModel.getCreditNo());
            }
            if (!TextUtils.isEmpty(companyModel.getMaintainUserName())) {
                binding.llContactInfo.setVisibility(View.VISIBLE);
                binding.tvContactInfo.setText(companyModel.getMaintainUserName());
            }
            if ((TextUtils.isEmpty(companyModel.getCreditNo()) || companyModel.getCreditNo().length() > 18) && TextUtils.isEmpty(companyModel.getMaintainUserName())) {
                binding.tvNoData.setVisibility(View.VISIBLE);
            }

            itemChooseCompanyBindingDataBindingHolder.itemView.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.TAG_KEY, 1);
                bundle.putString(Constant.CREDIT_NO_KEY, companyModel.getCreditNo());
                JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemMyCompanyBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_my_company, viewGroup);
    }
}
